//
//  DeviceInfo.m
//  gdemm
//
//  Created by 乔明超 on 16/5/31.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "DeviceInfo.h"
#import "Reachability.h"
#import <AdSupport/ASIdentifierManager.h>
//#include <sys/utsname.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#import <ALJailbreak.h>
#import <ALNetwork.h>
@implementation DeviceInfo

/*获取设备的IMEI*/
+ (NSString *)getIMEI{
    return nil;
}

/*获取设备的UUID*/
+ (NSString *)getUUID{
    return [[UIDevice currentDevice]identifierForVendor].UUIDString;
}

/*获取供应商ID*/
+ (NSString *)getIDForVendor{
    return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}

/*获取广告ID*/
+ (NSString *) getIdForAdvertising{
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}


/*获取系统名称和版本号*/
+ (NSString *) getOsVersion{
    return [NSString stringWithFormat:@"%@ %@",[UIDevice currentDevice].systemName,[UIDevice currentDevice].systemVersion];
}

/*获取设备具体型号*/
+ (NSString *)getDeviceModel{
    return [ALHardware platformType];
}

/*获取网络类型*/
+ (NSString *) getNetType{
    Reachability* reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (!reach)
    {
        return @"none";
    }
    
    NetworkStatus state = [reach currentReachabilityStatus];
    if (NotReachable == state)
    {
        return nil;
    }
    else if (ReachableViaWiFi == state)
    {
        return @"wifi";
    }
    else if (ReachableViaWWAN == state)
    {
        return @"mobile";
    }
    
    return @"none";
    
}

/*获取本地IP地址*/
+ (NSString *) getIpAddress{
    NSString *address = nil;
    
    struct ifaddrs* interfaces = 0;
    struct ifaddrs* temp_addr = 0;
    
    if (0 == getifaddrs(&interfaces))
    {
        temp_addr = interfaces;
        while (temp_addr)
        {
            if (AF_INET == temp_addr->ifa_addr->sa_family)
            {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if (0 == strcasecmp(temp_addr->ifa_name, "en0"))
                {
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in*)temp_addr->ifa_addr)->sin_addr)];
                    break;
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    if (interfaces)
    {
        freeifaddrs(interfaces);
    }
    
    return address;
}

/*获取设备的MAC地址*/
+ (NSString *) getMacAddress{
    return [ALNetwork macAddress];
}

/*获得设备的内存*/
+ (NSString *) getMemory{
    return [NSString stringWithFormat:@"%ld",[ALMemory totalMemory]];
}

/*获得蓝牙的地址*/
+ (NSString *) getBlueTooth{
    return [ALHardware bluetooth];
}

/*获取设备CPU的型号*/
+ (NSString *) getCPU{
   
    return [ALHardware cpu];

}

/*获取设备的相机的前置和后置摄像头的像素*/
+ (NSString *) getCamera{
    return [NSString stringWithFormat:@"后置 %@ \n 前置 %@",[ALHardware cameraPrimary],[ALHardware cameraSecondary]];

}
//主摄像头
+ (NSString *) getPriCamera{
    NSString *piex = [ALHardware cameraPrimary];
    NSInteger pie = [[piex substringWithRange:NSMakeRange(0, piex.length-2) ] intValue]*100;
    return [NSString stringWithFormat:@"%ld",pie];
}
//副摄像头
+ (NSString *) getSecCamera{
    NSString *piex = [ALHardware cameraSecondary];
    NSInteger pie = [[piex substringWithRange:NSMakeRange(0, piex.length-2) ] intValue]*100;
    return [NSString stringWithFormat:@"%ld",pie];

}

/*获取设备的屏幕分辨率*/
+ (NSString *) getScreenSize{
    int scale = [UIScreen mainScreen].scale;
    NSInteger width = [ALHardware screenWidth]*scale;
    NSInteger height = [ALHardware screenHeight]*scale;
    NSString *ppi = [ALHardware displayDensity];
    int density = [ppi substringWithRange:NSMakeRange(0, 3)].intValue;
    double screenSize = sqrt(width*width + height*height) / density;
    return [NSString stringWithFormat:@"%.1f",screenSize];

}

+ (NSString *) getResolution{
    int scale = [UIScreen mainScreen].scale;
    return [NSString stringWithFormat:@"%ld * %ld",[ALHardware screenWidth]*scale,[ALHardware screenHeight]*scale];
}

/*获取设备的存储容量*/
+ (NSString *) getDiskSpace{
    return [NSString stringWithFormat:@"%@",[ALDisk totalDiskSpace]];
}

/*获取设备是否越狱*/
+ (NSString *) getJailbrokenStatus{
    
    if ([ALJailbreak isJailbroken]) {
        return @"已越狱";
    }else{
        return @"未越狱";
    }
}
@end
